package unamur.iodass.data;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import unamur.iodass.communication.messages.CancelDeliver;
import unamur.iodass.communication.messages.ClientSerialised;
import unamur.iodass.communication.messages.ContratBookStore;
import unamur.iodass.host.HostHandler;

public class SoldBooks {
	private static final Logger logger = Logger.getLogger(ContratBookStore.class);

	private ArrayList<ContratBookStore> storedBooks;


	private static volatile SoldBooks instance;

	public static SoldBooks getInstance() {
		if (instance == null) {
			synchronized (SoldBooks.class) {
				if (instance == null) {
					instance = new SoldBooks();
				}
			}
		}
		return instance;
	}

	private SoldBooks() {
		storedBooks = new ArrayList<ContratBookStore>();
	}

	public void addContratBookStore(ContratBookStore contrat) {
		logger.info("Contrat ajouté au BookStore : " + 
				contrat.getBookStore().getName() + "\t " + contrat.getIsbn() + "\t prix : " + contrat.getMinimum());
		storedBooks.add(contrat);
	}

	public void remodeContratBookStore(CancelDeliver cancel) {

		for (int i=0; i<storedBooks.size(); i++) {
			if (storedBooks.get(i).getIsbn() == cancel.getIsbn()) {
				logger.info("Livre:\t "+ storedBooks.get(i).getIsbn() +
						" supprimé du BookStore :\t" + storedBooks.get(i).getBookStore().getName());


				storedBooks.remove(i);
			}
		}


	}

}
